/*********************************************************************
* Software License Agreement (BSD License)
* 
*  Copyright (c) 2017-2020, Waterplus http://www.6-robot.com
*  All rights reserved.
* 
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
* 
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the WaterPlus nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
* 
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  FOOTPRINTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/*!******************************************************************
 @author     ZhangWanjie
 ********************************************************************/

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sensor_msgs/JointState.h>

static bool bGrabDemo = false;
static ros::Publisher yolo_pub;
static std_msgs::String yolo_msg;
static sensor_msgs::JointState ctrl_msg;
static bool bInitedPose = false;
static int nWaitCount = 200;

static double fDegToAng = 3.1415926/180;
static double fAngToDeg = 180/3.1415926;

void JointStatesCallback(const sensor_msgs::JointState::ConstPtr& msg)
{
    int nNumJoint = msg->position.size();
    if(nNumJoint != 7)
    {
        //不是机械臂的tf
        return;
    }
    bool bAllPosArrived = true;
    for(int i=0;i<nNumJoint-1;i++)
    {
        float fPosDiff = fabs(ctrl_msg.position[i] - msg->position[i]*fAngToDeg);
        if(fPosDiff > 2.0)
        {
            bAllPosArrived = false;
            break;
        }
    }
    if(bAllPosArrived == true)
    {
        if(bInitedPose == false)
        {
            ROS_INFO("[yolo_grab_node] 姿态运动到位！");
            bInitedPose = true;
        }
    }
}

int main(int argc, char** argv)
{
    setlocale(LC_ALL,"");
    ros::init(argc, argv, "wpv4_mani_init2");

    //控制变量
    ctrl_msg.name.resize(7);
    ctrl_msg.position.resize(7);
    ctrl_msg.velocity.resize(7);
    //关节角度
    ctrl_msg.position[0] = 0;
    ctrl_msg.position[1] = 70;
    ctrl_msg.position[2] = 90;
    ctrl_msg.position[3] = 120;
    ctrl_msg.position[4] = -90;
    ctrl_msg.position[5] = 80;
    ctrl_msg.position[6] = 10000; //手爪张开
    //运动速度
    ctrl_msg.velocity[0] = 1000;
    ctrl_msg.velocity[1] = 1000;
    ctrl_msg.velocity[2] = 1000;
    ctrl_msg.velocity[3] = 1000;
    ctrl_msg.velocity[4] = 1000;
    ctrl_msg.velocity[5] = 1000;
    ctrl_msg.velocity[6] = 1000;

    //云台控制消息包
    sensor_msgs::JointState pt_ctrl_msg;
    pt_ctrl_msg.name.resize(2);
    pt_ctrl_msg.position.resize(2);
    pt_ctrl_msg.velocity.resize(2);
    //云台角度
    pt_ctrl_msg.position[0] = 0;
    pt_ctrl_msg.position[1] = 0;
    //云台运动速度
    pt_ctrl_msg.velocity[0] = 1000;
    pt_ctrl_msg.velocity[1] = 1000;

    ros::NodeHandle n;
    ros::Publisher joint_ctrl_pub = n.advertise<sensor_msgs::JointState>("wpm2/joint_ctrl_degree", 30);
    ros::Publisher pt_ctrl_pub = n.advertise<sensor_msgs::JointState>("/wpv4_pt/joint_ctrl_degree", 10);
    ros::Subscriber joint_states_sub = n.subscribe("/joint_states",10,&JointStatesCallback);
    yolo_pub = n.advertise<std_msgs::String>("/yolo_grab_obj", 10);
    
    ros::NodeHandle n_param("~");
    n_param.param<bool>("grab_demo", bGrabDemo, false);

    // ros::Duration(5).sleep();
    
    // joint_ctrl_pub.publish(ctrl_msg);
    // pt_ctrl_pub.publish(pt_ctrl_msg);

    // ros::Duration(0.3).sleep();

    // joint_ctrl_pub.publish(ctrl_msg);
    // pt_ctrl_pub.publish(pt_ctrl_msg);

    ros::Rate rate(10);
    while(ros::ok())
    {
        if( bInitedPose == false )
        {
            joint_ctrl_pub.publish(ctrl_msg);
            pt_ctrl_pub.publish(pt_ctrl_msg);
        }
        else
        {
            nWaitCount --;
            if(nWaitCount == 0)
            {
                if( bGrabDemo == true )
                {
                    yolo_msg.data = "pear";
                    yolo_pub.publish(yolo_msg);
                }
            }
        }
        rate.sleep();
        ros::spinOnce();
    }

    return 0;
}